package com.example.courseplus.Activity;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;

import com.example.courseplus.Adapter.ViewPagerAdapter;
import com.example.courseplus.DAO.StudentDAO;
import com.example.courseplus.Entity.Student;
import com.example.courseplus.Fragment.StudentCheck;
import com.example.courseplus.Fragment.StudentCourse;
import com.example.courseplus.Fragment.StudentInfo;
import com.example.courseplus.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class StudentActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private BottomNavigationView bottomNavigationView;
    private MenuItem menuItem;
    Toolbar toolbar;
    StudentCourse studentCourse;
    StudentCheck studentCheck;
    StudentInfo studentInfo;
    StudentDAO studentDAO = new StudentDAO();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student);
        viewPager = findViewById(R.id.student_pager);
        bottomNavigationView = findViewById(R.id.student_bnv);
        toolbar = findViewById(R.id.student_bar);
        toolbar.setTitle("你好，"+getUserName()+"同学");
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.stu_course:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.stu_check:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.stu_info:
                        viewPager.setCurrentItem(2);
                        break;
                    default:
                        break;
                }
                return false;
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(menuItem!=null){
                    menuItem.setChecked(false);
                }else{
                    bottomNavigationView.getMenu().getItem(0).setChecked(false);
                }
                menuItem = bottomNavigationView.getMenu().getItem(position);
                menuItem.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        setupViewPager(viewPager);
    }

    private String getUserName() {
         return getIntent().getStringExtra("username");
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),0);
        studentCourse = new StudentCourse();
        studentCheck = new StudentCheck();
        studentInfo = new StudentInfo();
        viewPagerAdapter.addFragment(studentCourse);
        viewPagerAdapter.addFragment(studentCheck);
        viewPagerAdapter.addFragment(studentInfo);
        viewPager.setAdapter(viewPagerAdapter);

    }

    public Student getStudent(){
        String userName = getUserName();
        return studentDAO.selectStudentByUserName(userName);
    }
}